String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
};

$(function(){
	//注册点击改变验证码事件
	$("#code").click(function(){
		var rand = Math.random();
		$("#rand").val(rand);
		$("#code").attr("src",CONTEXT_PATH + "/security/randomcode?rand="+rand);
	});
	
	$("#change").click(function(){
		var rand = Math.random();
		$("#rand").val(rand);
		$("#code").attr("src",CONTEXT_PATH + "/security/randomcode?rand="+rand);
	});
	
	//用户名只能是数字
	$("#username").keyup(function(){
		$(this).val($(this).val().replace(/[^\d]/g,""));
	});
	
	//初始化验证码
	$("#code").click();
	
	$(".red_card").click(function(){
		$("#cardId").focus();
	});
	
	$(document).bind("keydown","return",function(){
		onSubmit();
	});
	
	cookieDomain($('#domain'));
});

//取消
function cancle(){
	var $loginBtn	= $(".login_btn");
	var $tip		= $("#login_tip");
	$loginBtn.removeAttr("disabled");
	$tip.empty();
}

//用户登录提交
function onSubmit(){
	var username 	= $("#username");
	var code 		= $("#_code");
	var password	= $("#password");
	var cardId  	= $("#cardId").val();
	var rand		= $("#rand").val();
	var $tip		= $("#login_tip");
	var $loginBtn	= $(".login_btn");
	var domain		= $("#domain").attr("checked") ? true : false;
	
	var randomCode  = $("#randomCode").val();
	
	if($loginBtn.attr("disabled")){
		return false;
	}
	$loginBtn.attr("disabled",true);
	$tip.empty().html(LOADING_IMG+" 等待验证......").show();
	
	if($.trim(username.val()) == ""){
		$tip.hide().html("用户名不能为空!").fadeIn("normal");
		username.focus();
		$loginBtn.removeAttr("disabled");
		return false;
	}else if($.trim(username.val()).length != 6){
		$tip.hide().html("请输入正确的工号!").fadeIn("normal");
		username.focus();
		$loginBtn.removeAttr("disabled");
		return false;
	}
	if($.trim(password.val()) == ""){
		$tip.hide().html("密码不能为空!").fadeIn("normal");
		password.focus();
		$loginBtn.removeAttr("disabled");
		return false;
	}
	if($.trim(code.val()) == "" && randomCode != "firstlogin"){
		$tip.hide().html("验证码不能为空!").fadeIn("normal");
		code.focus();
		$loginBtn.removeAttr("disabled");
		return false;
	}
	
	if($.trim(cardId) == ""){
		cardId=1;
	}
	
	var encodedPwd = domain ? password.val() : MD5(password.val()+"sfpay").toUpperCase();
	var param = {username:username.val(),password:encodedPwd,code:code.val(),rand:rand,cardId:cardId,randomCode:randomCode,domain:domain};
	$.ajax({
		url:CONTEXT_PATH + "/security/login",
		data:param,
		dataType:'json',
		type:'POST',
		success:function(data){
			if(data.code == "01"){
				$tip.hide().html(data.message).fadeIn("normal");
				
				if(data.message.toString().indexOf("验证码") != -1){
					code.focus();
				}
				else if(data.message.toString().indexOf("用户名") != -1){
					username.focus();
					$("#rcode").show();
					$("#randomCode").val("UNfirstLogin");
				}
				
				$("#code").click();
				$loginBtn.removeAttr("disabled");
			}
			else if(data.code == "00"){
				$tip.hide().html(LOADING_IMG+" 验证通过......").fadeIn("normal");
				document.forms[1].submit();
			}
		}
	});
	/*$.getJSON($("#contextpath").val() + "/security/login",param,function(data){
		if(data.code == "01"){
			$tip.hide().html(data.message).fadeIn("normal");
			
			if(data.message.toString().indexOf("验证码") != -1){
				code.focus();
			}
			else if(data.message.toString().indexOf("用户名") != -1){
				username.focus();
			}
			
			$("#code").click();
			$loginBtn.removeAttr("disabled");
		}
		else if(data.code == "00"){
			$tip.hide().html(LOADING_IMG+" 验证通过......").fadeIn("normal");
			document.forms[1].submit();
		}
	});*/
};

